#! /bin/bash

set -e -x

HDP_VERSION=$(ls /usr/hdp/ | grep -e '^2\|^3')

sudo -E -u hdfs hadoop fs -mkdir -p /hdp/apps/${HDP_VERSION}/mapreduce/
sudo -E -u hdfs hadoop fs -put -f /usr/hdp/current/hadoop-client/mapreduce.tar.gz /hdp/apps/${HDP_VERSION}/mapreduce/
sudo -E -u hdfs hadoop fs -chown -R hdfs:hadoop /hdp
sudo -E -u hdfs hadoop fs -chmod -R 555 /hdp/apps/${HDP_VERSION}/mapreduce
sudo -E -u hdfs hadoop fs -chmod 444 /hdp/apps/${HDP_VERSION}/mapreduce/mapreduce.tar.gz

sudo -E -u hdfs hadoop fs -mkdir -p /mr-history/tmp 
sudo -E -u hdfs hadoop fs -chmod -R 777 /mr-history/tmp 
sudo -E -u hdfs hadoop fs -mkdir -p /mr-history/done 
sudo -E -u hdfs hadoop fs -chmod -R 777 /mr-history/done
sudo -E -u hdfs hadoop fs -chown -R $MAPRED_USER:$HDFS_USER /mr-history
sudo -E -u hdfs hadoop fs -mkdir -p /app-logs
sudo -E -u hdfs hadoop fs -chmod -R 777 /app-logs
sudo -E -u hdfs hadoop fs -chown $YARN_USER:$HDFS_USER /app-logs

sudo -E -u yarn /usr/hdp/current/hadoop-yarn-resourcemanager/sbin/yarn-daemon.sh --config $HADOOP_CONF_DIR start resourcemanager
sudo -E -u yarn /usr/hdp/current/hadoop-yarn-resourcemanager/sbin/yarn-daemon.sh --config $HADOOP_CONF_DIR start nodemanager
sudo -E -u yarn /usr/hdp/current/hadoop-mapreduce-historyserver/sbin/mr-jobhistory-daemon.sh --config $HADOOP_CONF_DIR start historyserver
